<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
import { ElMessageBox } from 'element-plus';
  const formLabelWidth = '140px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    formTitle:'',
    dialogTableVisible:false,
    dialogFormVisible:false,
    disabled:false,
    gift_id:'',
    gift_name:'',
    tableIndex:-1,
    list:[] as Array<any>
  })
  interface formQuert {
    id:number
    gift_id:number
    diamond_num:number
    multiple_num:number
    base_rate:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    diamond_num: [{ required: true, trigger: "blur", message: "请输入钻石数" }],
    multiple_num: [{ required: true, trigger: "blur", message: "请输入倍数" }],
    base_rate: [{ required: true, trigger: "blur", message: "请输入基础概率" }],
  }
  const handleForm = (query:any={})=>{
    const { row={}, $index=-1 } = query
    const { gift_id='', multiple_num='',
            diamond_num='', base_rate=''
          } = row
    action.tableIndex = $index
    action.formTitle = gift_id?'编辑礼物池':'新增礼物池'
    Object.assign(form,{
      gift_id,
      multiple_num,
      diamond_num,
      base_rate
    })
    action.dialogFormVisible = true
  }
  const handleDel = (query:any={})=>{
    const { $index=-1 } = query
    ElMessageBox.confirm(`是否删除此配置?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(() => {
      action.list.splice($index,1)
    })
    .catch(() => { });
  }
  const handleFormSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const index = action.list.findIndex(res=>res.multiple_num == form.multiple_num)
      if(index > - 1) return ToastWarning('当前倍数已存在，请勿重复新增')
      const { multiple_num, diamond_num, base_rate } = form
      if(action.tableIndex > -1) {
        Object.assign(action.list[action.tableIndex],{
          gift_id:action.gift_id,
          gift_name:action.gift_name,
          multiple_num,diamond_num,base_rate
        })
      } else {
        action.list.push({
          gift_id:action.gift_id,
          gift_name:action.gift_name,
          multiple_num,diamond_num,base_rate
        })
      }
      closeFormDialog()
    })
  }
  const handleSubmit = ()=>{
    const loading = Toastloading()
    propApi.setLuckGiftPoll({
      gift_id:action.gift_id,
      pool_list:JSON.stringify(action.list.map(res=>{
        const { multiple_num, diamond_num, base_rate } = res
        return {
          multiple_num,diamond_num,base_rate
        }
      }))
    }).then(res=>{
      ToastSuccess('保存成功')
      closeDialog()
      loading.close()
    }).catch(()=>{
      loading.close()
    })
  }
  const getList = async()=>{
    const res = await propApi.luckGiftPoolList({
      gift_id:action.gift_id
    })
    const { data=[] } = res
    action.list = data
  }
  const openDialog = async(query:any = {})=> {
    const { gift_id=0, name='' } = query
    action.gift_name = name
    action.gift_id = gift_id
    action.title = `${name}-礼物礼物池`
    const loading = Toastloading()
    await getList()
    loading.close()
    action.dialogTableVisible = true
  }
  const closeDialog = ()=>{
    action.dialogTableVisible = false
  }
  const closeFormDialog = ()=>{
    form = reactive({} as formQuert)

    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog"
    v-model="action.dialogTableVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="55%"
    :title="action.title">
      <el-button class="xm-mb10 xm-mrAuto" type="primary" @click="handleForm()">新增</el-button>
      <el-table
        class="xm-main xm-TableCenter"
        :data="action.list"
        border
      >
        <el-table-column width="80" prop="gift_id" label="礼物ID" />
        <el-table-column min-width="120" show-overflow-tooltip prop="gift_name" label="礼物名称" />
        <el-table-column sortable min-width="120" prop="diamond_num" label="钻石" />
        <el-table-column sortable min-width="120" prop="multiple_num" label="倍数" />
        <el-table-column sortable min-width="140" prop="base_rate" label="基础概率(万分位)" />
        <el-table-column width="160" label="操作">
          <template #default="scoped">
            <el-button type="primary" @click="handleForm(scoped)">编辑</el-button>
            <el-button type="danger" @click="handleDel(scoped)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="35%"
      :title="action.formTitle">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="钻石" prop="diamond_num">
          <el-input v-model="form.diamond_num" @input="allNumber(form,'diamond_num')" placeholder="请输入钻石数"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="倍数" prop="multiple_num">
          <el-input v-model="form.multiple_num" @input="allNumber(form,'multiple_num')" placeholder="请输入倍数"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="基础概率(万分位)" prop="base_rate">
          <el-input v-model="form.base_rate" @input="allNumber(form,'base_rate')" placeholder="请输入基础概率(万分位)"/>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeFormDialog">取 消</el-button>
          <el-button type="primary" @click="handleFormSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
